<template>
    <div>
        <div class="control">
            <!-- 车牌号 -->
            <div class="inputItem">
                <div class="title">车牌号</div>
                <div class="inputBox">
                    <el-input v-model="carNumber" @keyup.enter.native="getList1">
                    </el-input>
                </div>
            </div>
            <!-- 车型 -->
            <div class="inputItem">
                <div class="title">车型</div>
                <div class="inputBox">
                    <el-select class="select" v-model="TypeSelected" placeholder="请选择" @change="changeVehicleType($event)">
                        <el-option v-for="item in vehicleTypeData" :key="item.dictValue" :label="item.dictLabel"
                            :value="item.dictLabel">
                        </el-option>
                    </el-select>
                </div>
            </div>
            <div class="inputItem">
                <div class="title">创建时间</div>
                <el-date-picker size="small" v-model="createTime" type="datetimerange" :picker-options="pickerOptions"
                    range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" align="right"
                    :default-time="['00:00:00', '00:00:00']" value-format="yyyy-MM-dd HH:mm:ss">
                </el-date-picker>
            </div>
            <!-- 认证状态 -->
            <!-- <div class="inputItem">
        <div class="title">认证状态</div>
        <div class="inputBox">
          <el-select
            class="select"
            v-model="stateSelected"
            placeholder="请选择"
            @change="changeStateSelected($event)"
          >
            <el-option
              v-for="item in auditStatus"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </div>
      </div> -->
            <!-- 查询 -->
            <el-button type="primary" size="small" icon="el-icon-search" @click="getList1">查询</el-button>
            <el-button type="plain" size="small" icon="el-icon-refresh-right" @click="CleanUp">清空</el-button>
        </div>
        <div class="main">
            <div class="list">
                <el-table :data="records" style="width: 100%" ref="table3">
                    <el-table-column align="center" width="60px" prop="SerialNumber" label="序号">
                    </el-table-column>
                    <el-table-column prop="vehiclePhotos" align="center" label="车辆照片">
                        <template slot-scope="scope">
                            <el-image style="width: 65px; height: 48px;" :src="scope.row.carFortyFive"
                                :preview-src-list="[scope.row.carFortyFive]">
                            </el-image>
                        </template>
                    </el-table-column>
                    <el-table-column align="center" prop="numberplate" label="车牌号">
                    </el-table-column>
                    <el-table-column align="center" prop="vehicleType" label="车辆类型">
                    </el-table-column>
                    <el-table-column align="center" prop="brandModel" label="品牌型号">
                    </el-table-column>
                    <el-table-column align="center" prop="specification" label="规格">
                    </el-table-column>
                    <el-table-column align="center" prop="carriageLength" label="厢长">
                        <template slot-scope="scope">
                            <div>{{ scope.row.carriageLength == null ? '--' : scope.row.carriageLength + '米' }}</div>
                        </template>
                    </el-table-column>
                    <el-table-column align="center" prop="loadSide" label="载方">
                        <template slot-scope="scope">
                            <div>{{ scope.row.loadSide == null ? '--' : scope.row.loadSide + '方' }}</div>
                        </template>
                    </el-table-column>
                    <el-table-column align="center" prop="loadCapacity" label="载重">
                        <template slot-scope="scope">
                            <div>{{ scope.row.loadCapacity == null ? '--' : scope.row.loadCapacity + '吨' }}</div>
                        </template>
                    </el-table-column>
                    <el-table-column align="center" prop="vehicleHeight" label="车高">
                        <template slot-scope="scope">
                            <div>{{ scope.row.vehicleHeight == null ? '--' : scope.row.vehicleHeight + '米' }}</div>
                        </template>
                    </el-table-column>
                    <el-table-column align="center" prop="certStatus" label="认证状态">
                        <template slot-scope="scope">
                            <div class="stateBtns">
                                <el-button :class="scope.row.certStatus == 1 ? 'waitPass' : 'noPass'" size="small">
                                    <i class="iconfont icon-dian"></i>
                                    {{ scope.row.certStatus == 1 ? "待认证" : "不通过" }}
                                </el-button>
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column align="center" prop="createTime" label="创建时间"></el-table-column>
                    <el-table-column align="center" label="操作">
                        <template slot-scope="scope">
                            <div>
                                <!-- @click="$router.push('/certAudit/details/'+id)" -->
                                <el-button type="text" size="small" @click="
                                    $router.push('certAudit/details/' + scope.row.vehicleId)"
                                    v-if="permissions.getDetails2.perms != null">详情</el-button>
                                <!-- 0代表认证状态已经为不通过 -->
                                <el-button @click="showAuditDialog(scope.row.vehicleId)" type="text"
                                    v-if="scope.row.certStatus == 1 & permissions.certAudit.perms != null"
                                    size="small">认证</el-button>
                            </div>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <!-- 分页 -->
            <div class="paginationBox">
                <el-pagination class="pagination" background layout="slot,prev, pager, next" :total="data.total * 1"
                    :page-size="data.size * 1" :pager-count="5" @current-change="changePages($event)">
                    <template slot>
                        <span style="
                color: #686868;
                font-size: 12px;
                margin-right: 20px;
                display: inline-block;
              ">共{{ data.total }}条,每页{{ data.size }}条</span>
                    </template>
                </el-pagination>
            </div>
            <!-- 认证弹窗 -->
            <el-dialog class="dialog" title="认证" :visible.sync="visibleDialogBox" width="420px">
                <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="ruleForm">
                    <el-form-item label="认证结果" prop="certStatus">
                        <el-radio-group v-model="ruleForm.certStatus">
                            <el-radio :label="'2'">是</el-radio>
                            <el-radio :label="'3'">否</el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item label="认证说明" prop="auditFailReason" v-if="ruleForm.certStatus == 3">
                        <el-input type="textarea" resize="none" :rows="3" placeholder="请输入内容"
                            v-model="ruleForm.auditFailReason"></el-input>
                    </el-form-item>
                </el-form>
                <div style="display: flex; justify-content: flex-end">
                    <el-button class="cancelBtn" @click="visibleDialogBox = false">取 消</el-button>
                    <el-button class="submitBtn" type="primary" @click="SubmitAudit(ruleForm.certStatus)">确 定</el-button>
                </div>
            </el-dialog>
        </div>
    </div>
</template>

<script>
import httpApi from "@/http";

export default {
    data() {
        return {
            pickerOptions: {
                shortcuts: [{
                    text: '最近一周',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();
                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                        picker.$emit('pick', [start, end]);
                    }
                }, {
                    text: '最近一个月',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();
                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                        picker.$emit('pick', [start, end]);
                    }
                }, {
                    text: '最近三个月',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();
                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
                        picker.$emit('pick', [start, end]);
                    }
                }]
            },
            createTime: [],
            carNumber: "",
            TypeSelected: "", //当前选择 车型
            stateSelected: "1", //认证状态
            vehicleTypeData: [],
            visibleDialogBox: false, //是否显示认证弹窗
            ruleForm: {
                certStatus: "2",
                auditFailReason: "",
            },
            rules: {
                certStatus: [{ required: true, message: "请选择", trigger: "change" }],
                auditFailReason: [
                    { max: 200, message: "最大长度200 个字符", trigger: "blur" },
                ],
            },

            auditStatus: [
                {
                    value: "1",
                    label: "待认证",
                },
                {
                    value: "3",
                    label: "不通过",
                },
            ],
            data: {
                total: "",
                size: "",
                pages: "",
            },
            records: [],
            detailsData: {},
            pageCurrent: 1, //当前页码
            pageSize: "10", //每页10条
            vehicleId: "", //点击认证后 存放车辆ID再这
            permissions: {}
        };
    },
    created() {
        this.permissions = JSON.parse(sessionStorage.getItem('permissions'))
    },
    methods: {
        getSerialNumber() {
            // 前端解决序号
            this.records.forEach((item, i) => {
                let num = i + 1 + Number(this.pageSize) * (this.pageCurrent - 1);
                item.SerialNumber = num;
            });
        },
		getList1(){
			this.pageCurrent = 1;
			this.getList();
		},
        getList() {
            //初始化列表 及查询列表 一个接口
            let params = {
                portAddress: "PLATFORM",
                auditStatus: "0",
                numberplate: this.carNumber,
                vehicleType: this.TypeSelected,
                type: '0',//待认证车辆状态
                //   certStatus: this.stateSelected,

                current: this.pageCurrent,
                size: this.pageSize,
                startTime: this.createTime != null ? this.createTime[0] : '', //开始时间
                endTime: this.createTime != null ? this.createTime[1] : '', //结束时间
            };
            // console.log('传参',params);
            httpApi.vehicleApi.queryListIng(params).then((res) => {
                this.records = res.data.data.records;
                this.data = res.data.data;
                this.getSerialNumber();

            });
        },
        getVehicleType() {
            // 获取所有车辆种类
            httpApi.vehicleApi.getAllVehicleType().then((res) => {
                this.vehicleTypeData = res.data.data;
                // console.log('获取车辆种类', this.vehicleTypeData);
            });
        },
        //车型改变
        changeVehicleType(value) {
            this.TypeSelected = value;
        },
        // 认证状态改变
        changeStateSelected(value) {
            this.stateSelected = value;
        },

        // 清空
        CleanUp() {
            this.carNumber = "";
            this.stateSelected = "";
            this.TypeSelected = "";
            this.createTime = []
			this.pageCurrent = 1;
            this.getList();
        },
        // 页码改变时,更新列表数据
        changePages(val) {
            this.pageCurrent = val;
            this.getList();
        },
        showAuditDialog(vehicleId) {
            this.visibleDialogBox = true;
            this.vehicleId = vehicleId;
        },
        SubmitAudit(params) {
            // 2表示通过  3不通过
            this.showMessageBox = true;
            if (params == "2") {
                let params = { certStatus: "2", vehicleId: this.vehicleId };
                // console.log('params', params);
                httpApi.vehicleApi.VehicleCertified(params).then((res) => {
                    // console.log('通过结果', res);
                    // console.log('响应状态码',res.data.code);
                    if (res.data.code == 200) {
                        this.getList();
                        this.$message({
                            type: "success",
                            message: `认证通过! (后台:${res.data.msg})`,
                        });
                        this.visibleDialogBox = false;
                    }
                });
            } else if (params == "3") {
                let params = {
                    certStatus: "3",
                    auditFailReason: this.ruleForm.reauditFailReason,
                    vehicleId: this.vehicleId,
                };
                // console.log('params', params);
                httpApi.vehicleApi.VehicleCertified(params).then((res) => {
                    // console.log('不通过结果', res);

                    if (res.data.code == 200) {
                        this.getList();
                        this.$message({
                            type: "error",
                            message: `不通过认证! (后台:${res.data.msg})`,
                        });
                    }
                });
            }
        },
        // 记录滚动距离
        scrollEvent(res) {
            this.$route.meta.scrollTop = res.target.scrollTop
        },
    },
    activated() {
        //组件激活时监听表格滚动条位置,
        this.$refs.table3.bodyWrapper.addEventListener('scroll', this.scrollEvent, false)
        // 激活设置滚动位置
        this.$refs.table3.bodyWrapper.scrollTop = this.$route.meta.scrollTop
    },
    beforeRouteLeave(to, from, next) {
        // 为了节省资源，再离开页面的时候关闭对于scroll事件的监听
        this.$refs.table3.bodyWrapper.removeEventListener('scroll', this.scrollEvent)
        next()
    },


    mounted() {
        this.getList();
        this.getVehicleType();
    },
};
</script>

<style lang="scss" scoped>
.control {
    height: 76px;
    width: 100%;
    background-color: #fff;
    display: flex;
    align-items: center;
    padding: 0 29px;


    .inputItem {
        display: flex;
        align-items: center;
        margin-right: 23px;

        >.title {
            color: #262626;
            font-size: 12px;
            margin-right: 7px;
        }

        >.inputBox {
            width: 160px;

            ::v-deep .el-input__inner {
                height: 32px;
                font-size: 12px;
            }

            ::v-deep .el-input__inner::placeholder {
                color: #c3c3c3;
                font-size: 12px;
            }
        }

        ::v-deep .el-input__icon {
            line-height: 0;
        }
    }
}

.main {
    width: 100%;
    height: calc(100vh - 267px);
    background-color: #fff;


    .list {
        height: calc(100% - 48px);
        padding: 0 30px;

        ::v-deep .el-table__body-wrapper {
            height: calc(100vh - 362px);
            overflow-y: scroll;
        }

        // 表头样式
        ::v-deep .el-table__header th {
            background-color: #f0f6fd;
            color: #262626;
            font-size: 12px;
            font-weight: bold;
            text-align: center;

            // 表 body的td
            ::v-deep .el-table__body td {
                color: #595959;
                font-size: 12px;
            }
        }

        .stateBtns {
            height: 30px;
            width: 76px;
            margin: 0 auto;

            ::v-deep .el-button {
                height: 30px;
                width: 76px;
                font-size: 12px;
                border: none;
                display: flex;
                align-items: center;
                justify-content: center;
                line-height: 30px;
            }

            .waitPass {
                background-color: #ecf5fe;
                color: #1890ff;
            }

            .noPass {
                background-color: #fde8e9;
                color: #f7555d;
            }
        }
    }

    .paginationBox {
        border-top: 1px solid #f0f0f0;
        width: 100%;
        background-color: #fff;
        display: flex;
        height: 48px;
        align-items: center;
        padding: 0 30px;

        .pagination {
            margin-left: auto;
        }
    }
}

.dialog {
    ::v-deep .el-dialog__header {
        border-bottom: 1px solid #f0f0f0;
        padding: 15px 20px;
        color: #262626;
        font-size: 14px;
        font-weight: bold;
        height: 50px;
    }

    ::v-deep .el-dialog__title {
        font-size: 14px;
        color: #303133;
        font-weight: bold;
    }

    //关闭图标
    ::v-deep .el-dialog__headerbtn .el-dialog__close {
        font-size: 15px;
    }

    ::v-deep .el-dialog__body {
        padding: 20px;
    }
}
</style>
